<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="false"
    :okAuth="'maintenance:management:inMaintenanceInfo:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
    destroyOnClose={true}
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <Button id="equipmentDataSync" type="primary" @click="synEquipmentData" >同步设备数据</Button>
    <Button id="selectMaintenancer" type="primary" @click="selectMaintenancer" >选择维保人员</Button>
    <BasicForm @register="registerForm" />
    <Maintenancer :MaintenancerRecord="MaintenancerRecord" @register="registerMaintenancerModal" />
    <InfoList v-if="infoTid" :tid="infoTid"></InfoList>
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsMaintenanceManagementInMaintenanceInfoForm">
  import { ref, unref, computed, defineEmits,defineProps } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner} from '/@/components/Drawer';
  import { useModal, useModalInner,BasicModal } from '/@/components/Modal';
  import { InMaintenanceInfo, inMaintenanceInfoSave, inMaintenanceInfoForm } from '/@/api/maintenance.management/inMaintenanceInfo';
  import { synchronizerEquipmentData } from '/@/api/maintenance.management/inMaintenanceInfoData';
  import { officeTreeData } from '/@/api/sys/office';
  import { Button } from 'ant-design-vue';
  import InfoList from '../inMaintenanceInfoData/list.vue'
  import Maintenancer from './maintenancer.vue';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('maintenance.management.inMaintenanceInfo');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<InMaintenanceInfo>({} as InMaintenanceInfo);
  const infoTid=ref()
  const MaintenancerRecord = ref<any>();
  console.log('MaintenanceManagementInMaintenanceInfoForm',MaintenancerRecord)

 
  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增') : t('详情'),
  }));

  const sysEnterpriseId = ref<any>();
  const ManitenanceId=ref()
  const maintenanceEnterpriseId=ref()

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('任务标题'),
      field: 'title',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled:true
      },
    },
    {
      label: t('维保企业名称'),
      field: 'maintenanceEnterpriseId',
      fieldLabel:'maintenanceEnterpriseName',
      component: 'TreeSelect',
      componentProps: {
        maxlength: 255,
        api:officeTreeData,
        disabled:true
      },
    },
    {
      label: t('维保人员姓名'),
      field: 'userName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled:true
      },
    },
    {
      label: t('业主企业名称'),
      field: 'enterpriseName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled:true
      },
    },
    {
      label: t('状态'),
      field: 'state',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled:true
      },
    },
    {
      label: t('执行开始时间'),
      field: 'startExecuteTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        disabled:true
      },
    },
    {
      label: t('任务完成时间'),
      field: 'completeTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        disabled:true
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    let res='';
    res = await inMaintenanceInfoForm(data);
    console.log('res', res);
    MaintenancerRecord.value = res.inMaintenanceInfo;
    ManitenanceId.value=res.inMaintenanceInfo.tid
    maintenanceEnterpriseId.value=res.inMaintenanceInfo.enterpriseId
    console.log('MaintenancerRecord',MaintenancerRecord.value)
    infoTid.value=res.inMaintenanceInfo.tid
    // console.log('res', res.inMaintenanceInfo.tid);
    record.value = (res.inMaintenanceInfo || {}) as InMaintenanceInfo;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function synEquipmentData() {
    try{
      let data = await validate();
      // data={
      //   enterpriseId:ManitenanceId.value,
      //   maintenanceId:maintenanceEnterpriseId.value,
      // }
      console.log('maintenanceIdData', data);
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        // isNewRecord: record.value.isNewRecord,
        // tid: record.value.tid,
        enterpriseId:maintenanceEnterpriseId.value,
        maintenanceId:ManitenanceId.value,
      };
      // console.log('params', params);
      // console.log('submit', params, data, record);
      const res = await synchronizerEquipmentData(params,data);
      showMessage(res.message);
      setTimeout(closeDrawer);
    }catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }

  async function handleSubmit() {
    try {
      const data = await validate();
      sysEnterpriseId.value=data.enterpriseId
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
      };
      const res = await inMaintenanceInfoSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }

  const [registerMaintenancerModal, { openModal: openMaintenancerModal }] = useModal();
  function selectMaintenancer(){
    openMaintenancerModal();
  }
</script>

<style scoped lang="less">
#equipmentDataSync {
  margin-left: 40px;
  margin-bottom: 15px;
}
#selectMaintenancer{
  margin-left: 50px;
  margin-bottom: 15px;
}
</style>
